#!/usr/bin/env bash
# 将所有节点加入到cordon
NODE='k8s-host1  k8s-host2  k8s-host3  k8s-node1'
for node in $NODE
do
kubectl cordon $node
done
kubectl get nodes

# 备份原有的证书文件
IPS='192.168.1.28 192.168.1.29'

## 关闭kubelet docker
systemctl stop docker
systemctl stop kubelet
USER=root
for HOST in ${IPS}
do
ssh ${USER}@${HOST} systemctl stop docker
ssh ${USER}@${HOST} systemctl stop kubelet
done
# 备份k8s CA
mkdir -p /data/backup/CA/pki/etcd
for file in apiserver.crt apiserver.key apiserver-kubelet-client.crt apiserver-kubelet-client.key front-proxy-ca.crt front-proxy-ca.key front-proxy-client.crt front-proxy-client.key
do
mv /etc/kubernetes/pki/${file} /data/backup/CA/pki/
done

# 生成新证书
kubeadm init phase certs apiserver --config=kubeadm-config-init.yaml
kubeadm init phase certs apiserver-kubelet-client --config=kubeadm-config-init.yaml
kubeadm init phase certs front-proxy-ca --config=kubeadm-config-init.yaml
kubeadm init phase certs front-proxy-client --config=kubeadm-config-init.yaml
# 启动master节点
systemctl start docker
systemctl start kubelet

# 上传到其余master节点

for HOST in ${IPS}
do
scp -r /etc/kubernetes/pki ${USER}@${HOST}:/etc/kubernetes/
done
# 重启master
for HOST in ${IPS}
do
ssh ${USER}@${HOST} systemctl start docker
ssh ${USER}@${HOST} systemctl start kubelet
done


NODE='k8s-host1  k8s-host2  k8s-host3  k8s-node1'
for node in $NODE
do
kubectl uncordon $node
done
kubectl get nodes